package bg.cinema_system.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthorizationFilter implements Filter {

	FilterConfig config = null;
	ServletContext servletContext = null;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		config = filterConfig;
		servletContext = config.getServletContext();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpSession session = httpRequest.getSession();

		String requestPath = httpRequest.getRequestURI();
		String userId;
		String userRole;

		try {
			userId = (String) session.getAttribute("userId");
			userRole = (String) session.getAttribute("userRole");
		} catch (ClassCastException e) {
			userId = "" + (Integer) session.getAttribute("userId");
			userRole = "" + (Integer) session.getAttribute("userRole");

			if ("0".equals(userId)) {
				userId = null;
			}

			if ("0".equals(userRole)) {
				userRole = null;
			}
		}

		// System.out.println("ContextPath: " + httpRequest.getContextPath()
		// + "\nRequestPath: " + requestPath + "\nID: " + userId
		// + "\nRole: " + userRole);

		if (requestPath.endsWith("login.xhtml")
				|| requestPath.endsWith("registration.xhtml")) {
			if (userId == null || userRole == null) {
				chain.doFilter(request, response);
			} else {
				httpResponse.sendRedirect("user_daily_program.xhtml");
			}
		} else if (requestPath.endsWith("Cinema_Ticket_System/")) {
			if (userId == null || userRole == null) {
				httpResponse.sendRedirect("xhtml/login.xhtml");
			} else {
				httpResponse.sendRedirect("xhtml/user_daily_program.xhtml");
			}
		} else if (requestPath.endsWith("xhtml/")) {
			if (userId == null || userRole == null) {
				httpResponse.sendRedirect("login.xhtml");
			} else {
				httpResponse.sendRedirect("user_daily_program.xhtml");
			}
		} else {
			if (userId == null || userRole == null) {
				session.setAttribute("userId", 0);
				session.setAttribute("userRole", 0);
				httpResponse.sendRedirect(httpRequest.getContextPath()
						+ "/xhtml/login.xhtml");
			} else {
				if (!("1".equals(userRole) || "2".equals(userRole) || "3"
						.equals(userRole))) {
					httpResponse.sendRedirect(httpRequest.getContextPath()
							+ "/xhtml/login.xhtml");
				} else {
					if ("1".equals(userRole)) {
						if (requestPath.endsWith("admin.xhtml")
								|| requestPath.endsWith("staff.xhtml")) {
							httpResponse
									.sendRedirect("user_daily_program.xhtml");
						} else {
							chain.doFilter(request, response);
						}
					} else if ("2".equals(userRole)) {
						if (requestPath.endsWith("admin.xhtml")) {
							httpResponse
									.sendRedirect("user_daily_program.xhtml");
						} else {
							chain.doFilter(request, response);
						}
					} else {
						chain.doFilter(request, response);
					}
				}
			}
		}
	}

	@Override
	public void destroy() {

	}

}
